package Algorthim;

import java.util.LinkedList;
import java.util.Queue;

import searchTree.searchTreeNode;

public class QuqueBreadthFirst extends Algorithim{
	private Queue<searchTreeNode> phaseOne;
	private Queue<searchTreeNode> phaseTwo;
	private Boolean first;
	
	public QuqueBreadthFirst(boolean isFirst)
	{
		this.first = isFirst;
		this.phaseOne = new LinkedList<searchTreeNode>();
		this.phaseTwo = new LinkedList<searchTreeNode>();
	}
	
	@Override
	protected void handlePhaseOneReturn(searchTreeNode node, Integer firstleaf,
			Integer firstJunction, Integer secoundleaf,
			Integer secoundJunction, Integer thirdleaf, Integer forthleaf) {

		searchTreeNode optionOne = cloner.deepClone(node);
		optionOne.cutBranch(globaldata.getPathMap(),firstleaf, firstJunction);
		phaseOne.add(optionOne);
		
		searchTreeNode optionTwo = cloner.deepClone(node);
		optionTwo.cutBranch(globaldata.getPathMap(),secoundleaf, firstJunction);
		phaseOne.add(optionTwo);
		
		searchTreeNode optionThree = cloner.deepClone(node);
		optionThree.cutBranch(globaldata.getPathMap(),thirdleaf, secoundJunction);
		phaseOne.add(optionThree);
		
		searchTreeNode optionFour = cloner.deepClone(node);
		optionFour.cutBranch(globaldata.getPathMap(),forthleaf, secoundJunction);
		phaseOne.add(optionFour);
		
		if(first)
		{
			first = false;
			while(!phaseOne.isEmpty())
			{
				phaseOne(phaseOne.poll());
			}
			
			while(!phaseTwo.isEmpty())
			{
				phaseTwo(phaseTwo.poll());
			}
		}
		return;
		
	}

	@Override
	protected void handlePhaseOneComplete(searchTreeNode node) {
		this.createTranslation(node);
		phaseTwo.add(node);		
	}

	@Override
	protected void handlePhaseTwoReturn(searchTreeNode node, Integer[] edgeOne,
			Integer[] edgeTwo) {
		
		searchTreeNode nodeOne = cloner.deepClone(node);
		searchTreeNode nodeTwo = node;

		nodeOne.getTree().removeEdge(edgeOne[0], edgeOne[1]); 
		nodeTwo.getTree().removeEdge(edgeTwo[0], edgeTwo[1]);

		phaseTwo.add(nodeOne);
		phaseTwo.add(nodeTwo);
		
		phaseTwo(phaseTwo.poll());
		
	}

}
